<?php
namespace app\common\model\newmodel;
use think\Model;
use app\common\model\newmodel\Accessories as AccessoryModel;
class Device extends Model
{
    protected $table = 'device';
    protected $pk = 'id';
    
    /**
     * 关联工厂
     */
    public function factory()
    {
        return $this->belongsTo('app\\common\\model\\newmodel\\Factory','factory_id','id');
    }

    /**
     * 设备列表
     */
    public static function DeviceList($page=1, $pageSize=10, $keyword = '', $factory_id = '')
    {
        // 1. 构建查询并获取分页对象
        $query = self::with(['factory'])->where('id', '>', 1);
        
        if ($factory_id) {
            $query = $query->where('factory_id', $factory_id);
        }
        
        if ($keyword) {
            $query = $query->where('device_name', 'like', "%$keyword%");
        }
        
        $pagination = $query->paginate($pageSize, false, ['page' => $page]);
        
        $items = $pagination->items(); 
        $modifiedItems = [];
        foreach ($items as &$device) {
            $deviceArray = $device->toArray();
            // 处理配件信息
            $accessories = [];
            if (!empty($device->accessories_id)) {
                $accessoryIds = explode(',', $device->accessories_id);
                foreach ($accessoryIds as $id) {
                    $accessory = AccessoryModel::get($id);
                    if ($accessory) {
                        $accessories[] = $accessory->toArray();
                    }
                }
            }
            $deviceArray['accessories'] = $accessories;
            $modifiedItems[] = $deviceArray;
        }
        
        $pagination->data = $modifiedItems;
        return [
            'code' => 1,
            'msg' => 'success',
            'data' => $modifiedItems
        ];
    }

    /**
     * 添加设备
     */
    public static function addDevice($data)
    {
        if (empty($data['device_name']) || empty($data['factory_id']) || !isset($data['is_need'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }
            $device = new self();
            $device->device_name = $data['device_name'];
            $device->factory_id = $data['factory_id'];
            $device->is_need = $data['is_need'];
            if(!empty($data['accessories'])){
                $accessoriesStr = implode(',', $data['accessories']);
                $device->accessories_id=$accessoriesStr;
            }
            $device->save();
            return ['code' => 1, 'msg' => '设备添加成功'];
    }

    /**
     * 修改设备
     */
    public static function updateDevice($data)
    {
        if (empty($data['id']) || empty($data['device_name']) || empty($data['factory_id']) || !isset($data['is_need'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $device = self::get($data['id']);
        if (!$device) {
            return ['code' => 0, 'msg' => '设备不存在'];
        }

        try {
            $device->device_name = $data['device_name'];
            $device->factory_id = $data['factory_id'];
            $device->is_need = $data['is_need'];
            if(!empty($data['accessories'])){
                $accessoriesStr = implode(',', $data['accessories']);
                $device->accessories_id=$accessoriesStr;
            }
            $device->save();

            return ['code' => 1, 'msg' => '设备修改成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '设备修改失败：' . $e->getMessage()];
        }
    }

    /**
     * 删除设备
     */
    public static function deleteDevice($id)
    {
        if (empty($id)) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $device = self::get($id);
        if (!$device) {
            return ['code' => 0, 'msg' => '设备不存在'];
        }

        try {
            $device_id=$device->id;
            $device->delete();
            return ['code' => 1, 'msg' => '设备删除成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '设备删除失败：' . $e->getMessage()];
        }
    }
}